<?php
namespace jy_huolala;
defined('BY_JYA') or exit('error');
/*
	接口
	https://open.huolala.cn/#/doc/api?menu=9&type=2&id=5
*/
class city extends \plugin{
	public function upche($city_id=0){
		$cityinfo=$this->model('huo_api')->get_che($city_id);
		$vehicle_list=$cityinfo['vehicle_arr'];
		foreach($vehicle_list as $item){
			$isadd=pdo_get('jy_huolala_che',array('name'=>$item['name'],'city_id'=>$city_id,'uniacid'=>$_SESSION['uniacid']));
			$data=array(
				'order_vehicle_id'=>trim($item['order_vehicle_id']),
				'standard_order_vehicle_id'=>trim($item['standard_order_vehicle_id']),
				'vehicle_attr'=>trim($item['vehicle_attr']),
				'name'=>$item['name'],
				'img_url_high_light'=>trim($item['img_url_high_light']),
				'img_url_off_light'=>trim($item['img_url_off_light']),
				'carriage_length_cm'=>trim($item['carriage_length_cm']),
				'carriage_width_cm'=>trim($item['carriage_width_cm']),
				'carriage_height_cm'=>trim($item['carriage_height_cm']),
				'is_truck_direction'=>trim($item['is_truck_direction']),
				'surcharge_desc'=>trim($item['surcharge_desc']),
				'vehicle_price_text_item'=>ijson_encode($item['vehicle_price_text_item']),
				'vehicle_std_arr'=>ijson_encode($item['vehicle_std_arr']),
			);
			$id=intval($isadd['id']);
			if($id){
				pdo_update('jy_huolala_che',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}else{	
				$data['uniacid']=$_SESSION['uniacid'];
				$data['city_id']=$city_id;
				$data['createtime']=time();
				pdo_insert('jy_huolala_che',$data);
			}
		}
		$upcity=array(
			'revision'=>trim($cityinfo['revision']),
			'spec_req_arr'=>ijson_encode($cityinfo['spec_req_arr']),
		);
		pdo_update('jy_huolala_city',$upcity,array('city_id'=>$city_id,'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function upcity(){
		$cat_list=$this->model('huo_api')->get_city();
		if(!$cat_list){
			exi('接口异常！','error');
		}
		foreach($cat_list as $item){
			$isadd=pdo_get('jy_huolala_city',array('city_id'=>$item['city_id'],'uniacid'=>$_SESSION['uniacid']));
			$data=array(
				'name'=>trim($item['name']),
				'name_en'=>trim($item['name_en']),
				'city_id'=>trim($item['city_id']),
				'adcode'=>trim($item['adcode']),
				'lat_lng'=>trim($item['lat_lng']),
				'uptime'=>time(),
			);
			$id=intval($isadd['id']);
			if($id){
				pdo_update('jy_huolala_city',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}else{	
				$data['uniacid']=$_SESSION['uniacid'];
				$data['createtime']=time();
				pdo_insert('jy_huolala_city',$data);
			}
		}
		return true;
	}
}